﻿
@{
    ViewBag.Title = "SubjectVideo";
    Layout = "~/Views/Shared/_BCLayout.cshtml";
}

<fieldset class="layui-elem-field layui-field-title" style="margin-top: 20px;">
    <legend>视频绑定</legend>
</fieldset>
<form class="layui-form" id="FromData">
    <div class="layui-form-item">
        <label class="layui-form-label">视频选择</label>
        <div class="layui-input-block" style="z-index:5;">
            <select  name="VideoId" lay-verify="required" lay-search>
                <option value=""></option>
                @for (int i = 0; i < ViewBag.VideoList.Count; i++)
                {

                    <option value="@ViewBag.VideoList[i]["VideoId"]">@ViewBag.VideoList[i]["Title"]</option>
                }
            </select>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">排序时间</label>
        <div class="layui-input-inline">
            <input type="number" name="SortNum" id="SortNum" class="layui-input" required lay-verify="required" placeholder="剧集顺序，不要重复">
        </div>
        <div class="layui-input-inline">
            <button type="button" class="layui-btn" onclick="AddVideo()">添加</button>
        </div>
    </div>
    <button type="submit" id="mysubmit" class="layui-btn" lay-submit lay-filter="formVerify" style="display:none;">立即提交</button>
</form>
<table id="demo" lay-filter="VideoList"></table>

<script type="text/javascript">
    var SubjectId = "@ViewBag.SubjectId";
    var table;
    var IsValidate = 0;
    layui.use(['table', 'form', 'laydate', 'element'], function () {
        var form = layui.form;
        var laydate = layui.laydate;
        table = layui.table;
        form.render();
        table.render({
            elem: '#demo'
            , method: 'post'
            //, height: 315
            , url: '/Home/SVTableRequest' //数据接口
            , page: true //开启分页
            , where: { SortName: "SortNum", Order: "Desc", Condition: " and SubjectId=" + SubjectId }
            , limits: [50, 100, 200]
            , limit: 50
            , cols: [[ //表头
                { field: 'Title', title: '标题', width: 600 }
                , {
                    field: "operation", title: "操作", width: 200,
                    templet: function (d) {
                        return '<a class="layui-btn layui-btn-xs" lay-event="Delete">删除</a>';
                    }
                }
            ]]
        });

        table.on("tool(VideoList)", function (obj) {
            var data = obj.data; //获得当前行数据
            var layEvent = obj.event; //获得 lay-event 对应的值（也可以是表头的 event 参数对应的值）
            var tr = obj.tr; //获得当前行 tr 的DOM对象

            //GetArticle
            if (layEvent == 'Delete') {
                //alert(data.ArticleId);
                $.ajax({
                    type: "POST",
                    url: "/Home/API_SubjectVideoDel",
                    async: false,
                    data: { "SVId": data.SVId },
                    success: function (msg) {
                        if (msg.result == "OK") {
                            table.reload("demo");
                        } else {
                            alert("删除失败");
                        }
                    }
                });
            }
        });


        //监听提交
        form.on('submit(formVerify)', function (data) {
            //layer.msg(JSON.stringify(data.field));
            IsValidate = 1;
            return false;
        });

    });

    function AddVideo() {
        IsValidate = 0;
        $("#mysubmit").click();
        if (IsValidate == 1) {
            IsValidate = 0;
            //alert("表单验证通过");
            var obj = $("#FromData").serializeArray();
            var data = {};
            $.each(obj, function (i, v) {
                data[v.name] = v.value;
            });
            data["SubjectId"] = SubjectId;
            $.ajax({
                type: "POST",
                url: "/Home/API_SubjectVideoAdd",
                async: false,
                contentType: "application/json",
                dataType: "json",
                data: JSON.stringify(data),
                success: function (msg) {
                    if (msg.result == "OK") {
                        
                        table.reload("demo");
                    } else {
                        alert("新增失败");
                    }
                }
            });

        } else {
            return;
        }

    }




</script>